package com.hades.web;

import com.hades.pojo.Book;
import com.hades.pojo.Page;
import com.hades.service.BookService;
import com.hades.service.impl.BookServiceImpl;
import com.hades.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientBookServlet extends BaseServlet{
    private BookService bookService = new BookServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //获取page对象
        Page<Book> page = bookService.page(pageNo,pageSize);
        //设置url
        page.setUrl("client/bookServlet?action=page");
        //保存到request域中
        req.setAttribute("page",page);
        //跳转
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }

    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        int min = WebUtils.parseInt(req.getParameter("min"),0);
        int max = WebUtils.parseInt(req.getParameter("max"),Integer.MAX_VALUE);
        //获取page对象
        Page<Book> page = bookService.pageByPrice(pageNo,pageSize,min,max);
        StringBuffer url = new StringBuffer("client/bookServlet?action=pageByPrice");
        if(req.getParameter("min") != null){
            url.append("&min=").append(min);
        }
        if(req.getParameter("max") != null){
            url.append("&max=").append(max);
        }
        //设置url
        page.setUrl(url.toString());
        //保存到request域中
        req.setAttribute("page",page);
        //跳转
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
}
